package cn.iocoder.yudao.module.system.controller.admin.tradinghours;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.system.controller.admin.tradinghours.vo.SystemTradingHoursListReqVO;
import cn.iocoder.yudao.module.system.controller.admin.tradinghours.vo.SystemTradingHoursPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.tradinghours.vo.SystemTradingHoursRespVO;
import cn.iocoder.yudao.module.system.controller.admin.tradinghours.vo.SystemTradingHoursSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.tradinghours.SystemTradingHoursDO;
import cn.iocoder.yudao.module.system.service.tradinghours.SystemTradingHoursService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 营业时间")
@RestController
@RequestMapping("/system/trading-hours")
@Validated
public class SystemTradingHoursController {

    @Resource
    private SystemTradingHoursService tradingHoursService;

    @PostMapping("/create")
    @Operation(summary = "创建营业时间")
    @PreAuthorize("@ss.hasPermission('system:trading-hours:create')")
    public CommonResult<Long> createTradingHours(@Valid @RequestBody SystemTradingHoursSaveReqVO createReqVO) {
        return success(tradingHoursService.createTradingHours(createReqVO));
    }

    @PostMapping("/save-weekly")
    @Operation(summary = "保存每周营业时间")
    @PreAuthorize("@ss.hasPermission('system:trading-hours:create')")
    public CommonResult<List<SystemTradingHoursRespVO>> saveWeeklyTradingHours(@Valid @RequestBody List<SystemTradingHoursSaveReqVO> saveReqVOS) {
        return success(tradingHoursService.saveWeeklyTradingHours(saveReqVOS));
    }

    @PutMapping("/update")
    @Operation(summary = "更新营业时间")
    @PreAuthorize("@ss.hasPermission('system:trading-hours:update')")
    public CommonResult<Boolean> updateTradingHours(@Valid @RequestBody SystemTradingHoursSaveReqVO updateReqVO) {
        tradingHoursService.updateTradingHours(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除营业时间")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:trading-hours:delete')")
    public CommonResult<Boolean> deleteTradingHours(@RequestParam("id") Long id) {
        tradingHoursService.deleteTradingHours(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得营业时间")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:trading-hours:query')")
    public CommonResult<SystemTradingHoursRespVO> getTradingHours(@RequestParam("id") Long id) {
        SystemTradingHoursDO tradingHours = tradingHoursService.getTradingHours(id);
        return success(BeanUtils.toBean(tradingHours, SystemTradingHoursRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得营业时间分页")
    @PreAuthorize("@ss.hasPermission('system:trading-hours:query')")
    public CommonResult<PageResult<SystemTradingHoursRespVO>> getTradingHoursPage(@Valid SystemTradingHoursPageReqVO pageReqVO) {
        PageResult<SystemTradingHoursDO> pageResult = tradingHoursService.getTradingHoursPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SystemTradingHoursRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出营业时间 Excel")
    @PreAuthorize("@ss.hasPermission('system:trading-hours:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTradingHoursExcel(@Valid SystemTradingHoursPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SystemTradingHoursDO> list = tradingHoursService.getTradingHoursPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "营业时间.xls", "数据", SystemTradingHoursRespVO.class,
                        BeanUtils.toBean(list, SystemTradingHoursRespVO.class));
    }
    
    @PostMapping("/list")
    @Operation(summary = "获取营业时间列表")
    @PreAuthorize("@ss.hasPermission('system:trading-hours:query')")
    public CommonResult<List<SystemTradingHoursRespVO>> getTradingHoursList(@Valid @RequestBody SystemTradingHoursListReqVO reqVO) {
        List<SystemTradingHoursRespVO> list = tradingHoursService.getTradingHoursList(
                reqVO.getType(),  reqVO.getModule(), reqVO.getStartDate(), reqVO.getEndDate());
        return success(list);
    }

}